package com.kk.goGame.service.impl;

import com.kk.goGame.entity.Orders;
import com.kk.goGame.mapper.OrdersMapper;
import com.kk.goGame.mapper.impl.OrdersMapperImpl;
import com.kk.goGame.service.IOrdersService;
import com.kk.goGame.service.IUserService;
import com.qf.framwork.annotation.RequestParams;
import com.qf.framwork.utils.ConnectionUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author kk
 * @since 2023-02-14
 */
public class OrdersServiceImpl implements IOrdersService {
    private OrdersMapper ordersMapper = new OrdersMapperImpl();
    private IUserService userService = new UserServiceImpl();
    @Override
    public String createrOders(Integer id, Integer price) {
        String oid = UUID.randomUUID().toString().replace("-","");

        Orders orders = new Orders()
                .setOid(oid)
                .setUid(id)
                .setPrice(price)
                .setScore(price*100);
        ordersMapper.insert(orders);
        return oid;
    }

    @Override
    public Orders checkColumn(String column, Object value) {
        return ordersMapper.checkColumn(column,value);
    }

    /**
     * 修改订单状态业务
     * @param order
     * @return
     */
    @Override
    public Integer updateOrdersStatus(Orders order) {
        try {
            ConnectionUtils.begin();

            //修改订单状态
            int i = ordersMapper.updateOrdersStatus(order);
            if (i == 1) {
                //支付成功
                if (order.getStatus() == 1) {
                    //订单状态修改成功，修改对应用户的欢乐豆
                    Orders orders = ordersMapper.checkColumn("oid",order.getOid());
                    Integer uid = orders.getUid();//下单者
                    Integer score = orders.getScore();//充值积分值

                    //修改用户积分
                    userService.updateScore(uid, score, order.getOid(), 0);
                }
            }
            ConnectionUtils.commit();
        } catch (SQLException e) {
            ConnectionUtils.rollback(e);
            e.printStackTrace();
        }
        return 1;
    }

    @Override
    public Map<Date,Integer> tongjiPrice(@RequestParams("id") Integer uid) {
        return ordersMapper.tongjiPrice(uid);
    }

    @Override
    public List<Orders> getAll(Integer uid) {
        return ordersMapper.getAll(uid);
    }
}
